﻿/**************************使用者保留信息***********************/
/*无限级下拉框插件                                             */
/* 开发者：王继辉										       */
/* 时间：2014-05-15  									       */
/* 网址：                                                      */
/* 感谢：www.922197.com                                        */
/* 联系：394594745@qq.com                                      */
/**************************使用者保留信息***********************/
(function ($) { 	 
    // 插件的定义    
    $.fn.NoLimitSelect = function (options) {
        // build main options before element iteration    
       var opts = $.extend({}, $.fn.NoLimitSelect.defaults, options);
        // iterate and reformat each matched element    
       return this.each(function () { 
	       //debugger;
			var $this = $(this); 
            // build element specific options    
            var $opts = $.meta ? $.extend({}, opts, $this.data()) : opts;  
			 
			 //Plugin start
			$this.html($.fn.NoLimitSelect.createTage($opts)); 
			 
			var _cid = $("#"+$opts.HidCurName+ $opts.TName+$opts.TageID).val();
			if (!_cid || typeof (_cid) == "undefined" || _cid==""|| _cid=="0"){ 
				 $.fn.NoLimitSelect.data_load($opts,0);
			}else {
				$("#" +$opts.HidPreName+ $opts.TName+$opts.TageID).val(_cid);
				$.fn.NoLimitSelect.dataInit($opts,_cid);
			}  
			return this;
        });
    }; 
	
	// 插件的defaults    
    $.fn.NoLimitSelect.defaults = {  
		 TName:"area", 
		 Val:'areaid',
		 Text:'areaname',		 
		 SOptionTxt:'请选择',
		 SOptionVal:'-2',
		 TitleName:"区域：",
		 SVal:0,
		 PName:'aid',
		 //定义标签区别多个调用 
		 TageID:"tageid", 
		 HidPreName:'hidden_',
		 HidCurName:'curhidden_',
		 TagePre:'span_',
		 QueryUrl:'/getdata.php', 
		 List:'json',
		 Info:'Info'
    };
	
	//创建承载标签
	$.fn.NoLimitSelect.createTage=function(setting){ 
		var rel = $("&nbsp;<a href='javascript:;'>重选</a>").bind("click",function(){
   		   $.fn.NoLimitSelect.data_reload(setting);
     	});		
	    var _html= (setting.TitleName==''?"":"<span>"+setting.TitleName +"</span>") + "<input type='hidden' id='"+setting.HidCurName + setting.TName+setting.TageID+"' value='"+setting.SVal+"' /><input type='hidden' id='"+setting.HidPreName+ setting.TName+setting.TageID+"' name='"+setting.PName+"' value='"+setting.SVal+"' />"
		return $("<span/>").attr("id",setting.TagePre + setting.TName+setting.TageID).after(rel).before(_html); 
	};
	
	//重选操作
	$.fn.NoLimitSelect.data_reload=function(setting){
		  $("#"+setting.TagePre + setting.TName+setting.TageID).html('');
		  $("#" +setting.HidPreName+ setting.TName+setting.TageID).val('');
		  $.fn.NoLimitSelect.data_load(setting,0);
	};
	
	//加载信息
	$.fn.NoLimitSelect.data_load=function(setting,id) {  //debugger;
		if (id == setting.SOptionVal) { $("#" +setting.HidPreName+ setting.TName+setting.TageID).val(''); return; } 
		$.ajax({ 
			url:setting.QueryUrl, 
			data:{deftx:setting.SOptionTxt,defval:setting.SOptionVal, field: setting.HidPreName + setting.TName+setting.TageID, id: id, fun: "",o:setting.List,n:setting.TName,v:setting.Val,t:setting.Text,tageid:setting.TageID},
			type: "POST", 
			dataType:'json', 
			success:function(data) {   
			 //debugger; 
             var sel=$("<select/>").bind("change",function(){$.fn.NoLimitSelect.datachange(setting,$(this));}).append("<option value='"+setting.SOptionVal+"'>"+setting.SOptionTxt+"</option>");
			 if(data){
				 $.each(data.JSON,function(entryIndex,entry){
					 sel.append("<option value='"+entry[setting.Val]+"'>"+entry[setting.Text]+"</option>");
				  });
				 $("#"+setting.TagePre + setting.TName+setting.TageID).append(sel);
			 } 
		    },
			error:function(er){
				//debugger; 
				alert(er.reponseText);
			  } 
			});  
	}; 
	
   //信息初始化
   $.fn.NoLimitSelect.dataInit=function(setting,_val) {
		$.post(setting.QueryUrl, {id: _val,o:setting.Info,n:setting.TName,v:setting.Val,t:setting.Text,tageid:setting.TageID},
		  function(data) { 
			  if (data == "")
				  $.fn.NoLimitSelect.data_load(setting,0);
			  else
				 $("#" +setting.TagePre+ setting.TName+setting.TageID).append(data);
		  });
	};
	
   //选择操作
   $.fn.NoLimitSelect.datachange=function(setting,obj){
	  obj.attr("disabled",true);
	  var _v=obj.val();
	  $("#" +setting.HidPreName+ setting.TName+setting.TageID).val(_v);
	  $.fn.NoLimitSelect.data_load(setting,_v);
   };
   
    // 闭包结束    
})(jQuery);
 